import pointGoodsApi from '../../api/pointGoodsApi'
import pointApi from '../../api/pointApi'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    goods: '',
    visible: false,
    selectList: [],
    realPoint: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getDetail(options._id)
  },

  async getDetail(_id) {
    let res = await pointGoodsApi.get(_id)
    this.setData({
      goods: res
    })
  },

  onVisibleChange(e) {
    this.setData({
      visible: e.detail.visible,
    });
  },
  openPopup() {
    this.setData({
      visible: true
    })
    for (let i = 0; i < this.data.goods.enumList.length; i++) {
      this.data.selectList[i] = this.data.goods.enumList[i].itemList[0]
    }
    let tempPoint = 0
    for (let item of this.data.selectList) {
      if (item.value > tempPoint) {
        tempPoint = item.value
      }
    }
    this.setData({
      selectList: this.data.selectList,
      realPoint: tempPoint
    })
  },

  selectEnum(e) {
    let enumIndex = e.currentTarget.dataset.enumIndex
    let index = e.currentTarget.dataset.index

    let item = this.data.goods.enumList[enumIndex].itemList[index]
    this.data.selectList[enumIndex] = item
    console.log(this.data.selectList)

    let tempPoint = 0
    for (let item of this.data.selectList) {
      if (item.value > tempPoint) {
        tempPoint = item.value
      }
    }
    this.setData({
      selectList: this.data.selectList,
      realPoint: tempPoint
    })
  },
  confirm() {
    for (let i = 0; i < this.data.goods.enumList.length; i++) {
      if (!this.data.selectList[i]) {
        wx.showToast({
          title: '请选择' + this.data.goods.enumList[i].title,
          icon: 'none'
        })
        return
      }
    }
    this.setData({
      visible: false
    })

    this.exchange()
  },

  async exchange() {
    wx.showLoading()
    let res = await pointApi.get({
      openid: wx.getStorageSync('openid')
    })
    wx.hideLoading()
    if (!res) {
      wx.showToast({
        title: '积分不够',
        icon: 'none'
      })
      return
    }
    if (res.count < this.data.realPoint) {
      wx.showToast({
        title: '积分不够',
        icon: 'none'
      })
      return
    }
    let orderItem = {
      goodsId: this.data.goods._id,
      title: this.data.goods.title,
      imgList: this.data.goods.imgList,
      point: this.data.realPoint,
      enumList: this.data.selectList
    }
   
    wx.setStorageSync('orderItem', orderItem)
    wx.navigateTo({
      url: `/pointShop/pointGoodsAddress/pointGoodsAddress`,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    var obj = {
      title: this.data.goods.title,
      path: "/pointShop/pointGoodsDetail/pointGoodsDetail?_id=" + this.data.goods._id
    }
    if (this.data.goods.imgList.length > 0) {
      obj.imageUrl = this.data.goods.imgList[0]
    }
    return obj
  }
})